using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Pampa.Grid.Core.Filters;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.UI.CustomControls;

using Pampa.Grid.Exceptions;

namespace Pampa.Grid.Core
{
    public class QuickFilterDefinition
    {
        private EditItem item;
        private string field;
        private OperationType operationType;
        private Type valueType;

        public QuickFilterDefinition()
        {
            operationType = OperationType.Equal;
        }
        
        public EditItem Item
        {
            get { return item;}
            set { item = value; }
        }

        public string Field
        {
            get { return field; }
            set { field = value; }
        }

        public OperationType OperationType
        {
            get { return operationType; }
            set { operationType = value; }
        }

        public Type ValueType
        {
            get { return valueType; }
            set { valueType = value; }
        }

        public static QuickFilterDefinition LoadFromXml(XmlNode node)
        {
            QuickFilterDefinition quickFilterDefinition = new QuickFilterDefinition();

            quickFilterDefinition.Item = EditItem.LoadFromXml(node);
            quickFilterDefinition.Item.Enabled = true;

            if (node.Attributes["Field"] != null)
            {
                quickFilterDefinition.Field = node.Attributes["Field"].Value;
            }
            
            if (node.Attributes["OperationType"] != null)
            { 
                quickFilterDefinition.OperationType = (OperationType)Enum.Parse(typeof(OperationType), node.Attributes["OperationType"].Value);
            }

            if (node.Attributes["ValueType"] != null)
            {
                quickFilterDefinition.ValueType = Type.GetType(node.Attributes["ValueType"].Value);
            }

            if (quickFilterDefinition.ValueType == null)
            {
                quickFilterDefinition.ValueType = quickFilterDefinition.Item.ValueTypeDefault;
            }
            
            return quickFilterDefinition;
        }

    }
}
